//
//  OAuthViewController.swift
//  微博
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 itcats. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking
class OAuthViewController: UIViewController {

    let webView = UIWebView()
    
    @objc private func close() {
    dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func defaultAccount() {
    
        let jsString = "document.getElementById('userId').value = 'kkla888@sina.cn' ,document.getElementById('passwd').value = '6167890' "
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    
    }
    
    override func loadView() {
        view = webView
        webView.delegate = self
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
       loadOAuthPage()
        setNavBar()
        
    }
    //https://api.weibo.com/oauth2/authorize?client_id=473346128&redirect_uri=www.baidu.com
    //https://api.weibo.com/oauth2/authorize?client_id=473346128&redirect_uri=https://www.baidu.com
    //加载授权界面
    private func loadOAuthPage() {
       let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        let url = NSURL(string: urlString)
        print(url)
        webView.loadRequest(NSURLRequest(URL: url!))
        
    }
    

    private func setNavBar() {
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "defaultAccount")
    
    }
    
}

extension OAuthViewController: UIWebViewDelegate {
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
        
    }
    //希望加载
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //屏蔽不希望加载的页面
        guard let urlString = request.URL?.absoluteString else{
        return false
        }
        if urlString.hasPrefix("https://api.weibo.com/"){
        return true
        }
        if !urlString.hasPrefix("http://www.baidu.com"){
        return false
        }
          print(urlString)
        let query = request.URL?.query
        if let q = query{
        let codeStr = "code="
        let code = q.substringFromIndex(codeStr.endIndex)
        loadAccessToken(code)
           
        }
        return true
    }
    private func loadAccessToken(code:String){
       UserAccountViewModel().loadAccessToken(code) { (isSuccess) -> () in
        if isSuccess{
        print("登陆成功")
      self.dismissViewControllerAnimated(false, completion: { () -> Void in
          NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: "Welcome")
      })
       
        }else{
        print("登陆失败")
        }
        
    }
    
    }
}
